package com.woniu.finance.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.finance.entity.Finance;
import com.woniu.finance.entity.Records;
import com.woniu.finance.entity.RequestParam;
import com.woniu.finance.entity.vo.RecordsVO;
import com.woniu.finance.mapper.RecordsMapper;
import com.woniu.finance.service.IRecordsService;
import com.woniu.util.utils.HttpResult;
import net.bytebuddy.matcher.RecordMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecordsServiceImpl implements IRecordsService {

    @Autowired(required = false)
    private RecordsMapper recordsMapper;
    @Override
    public HttpResult<?> saveRecordsMenu(Records records) {
        try {
            recordsMapper.insert(records);
            return new HttpResult<>().ok();
        }catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> removeRecordsMenu(Integer id) {
        try {
            recordsMapper.deleteById(id);
            return new HttpResult<>().ok();
        }catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> modifyRecordsMenu(Records records) {
        try {
            recordsMapper.updateById(records);
            return new HttpResult<>().ok();
        }catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> findAllByPage(RequestParam<RecordsVO> param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<RecordsVO> allByPage = recordsMapper.findAllByPage(param.getData().getFinanceName(),param.getData().getCoreName(),param.getData().getRecordType());
        PageInfo pageInfo = new PageInfo(allByPage);
        return new HttpResult<>().ok(pageInfo);
    }

    @Override
    public HttpResult<?> findRecordsMenuById(Integer id) {
        try {
            Records records = recordsMapper.selectById(id);
            return new HttpResult<>().ok(records);
        }catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> findRecordsByFinanceId(RequestParam<Records> param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Records> allByPage = recordsMapper.findAllByFinanceId(param.getData().getFinanceId());
        PageInfo pageInfo = new PageInfo(allByPage);
        return new HttpResult<>().ok(pageInfo);
    }
}
